<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客注册页</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <style>
        .container-register {
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .register-dialog {
            background: #fff;
            padding: 20px;
            border-radius: 5px;
        }
        .register-dialog h3 {
            text-align: center;
            margin-bottom: 20px;
        }
        .row {
            margin-bottom: 15px;
        }
        .row label {
            display: block;
            margin-bottom: 5px;
        }
        .row input[type="text"],
        .row input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .row .error {
            color: red;
            margin-top: 5px;
        }
        .row button {
            width: 100%;
            padding: 10px;
            border: none;
            background-color: #007bff;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }
        .row button:hover {
            background-color: #0056b3;
        }
        .register-btn {
            display: block;
            margin: 10px 0;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="container-register">
        <div class="register-dialog">
            <h3>注册</h3>
            <form id="registrationForm">
                <div class="row">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="row">
                    <label for="password">密码:</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <div class="row">
                    <label for="confirmPassword">重复密码:</label>
                    <input type="password" id="confirmPassword" name="confirmPassword" required>
                    <div id="passwordError" class="error"></div>
                </div>
                <div class="row">
                    <button type="submit">注册</button>
                </div>
            </form>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        $('#registrationForm').on('submit', function(e) {
            e.preventDefault(); // 阻止表单默认提交行为
            $('#passwordError').text(''); // 清除之前的错误信息

            var password = $('#password').val();
            var confirmPassword = $('#confirmPassword').val();
            if (password !== confirmPassword) {
                $('#passwordError').text('密码和重复密码不一致，请重新输入！');
                return; // 退出函数，不进行提交
            }

            // 密码一致，使用 AJAX 发送数据到后端
            var userData = {
                userName: $('#username').val(),
                password: password
            };

            $.ajax({
                type: 'POST',
                url: '/user/register', // 后端注册接口的URL
                data: JSON.stringify(userData),
                contentType: 'application/json',
                success: function(response) {
                    console.log('Registration successful:', response);
                    if (response.data == true) {
                        alert('注册成功！去登录->');
                        location.href = "blog_login.html";
                    } else {
                        alert('注册失败,用户名已存在!');
                    }
                },
                error: function(xhr, status, error) {
                    console.error('Registration failed:', status, error);
                    alert('注册失败，请联系管理员。');
                }
            });
        });
    </script>
</body>

</html>